package com.tcm.doctor.service;

import cn.hutool.core.util.ObjectUtil;
import com.base.core.dozer.DozerConvert;
import com.base.core.lang.DateUtils;
import com.base.genid.generator.UidGenerator;
import com.tcm.doctor.domain.dto.BankAccountDTO;
import com.tcm.doctor.domain.entity.BankAccount;
import com.tcm.doctor.mapper.BankAccountMapper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;


@DubboService(version = "0.0.1-alpha")
public class BankAccountServiceImpl implements BankAccountService {

    @Autowired
    private BankAccountMapper bankAccountMapper;

    @DubboReference(version = "0.0.1-beta", group = "cached-uid", check = false, retries = 3)
    private UidGenerator uidGenerator;


    /**
     * 单条记录查询
     *
     * @param userId
     * @return
     */
    @Override
    public BankAccountDTO findById(Long userId) {
        BankAccount bankAccount = bankAccountMapper.selectByUserId(userId);
        BankAccountDTO bankAccountDTO = null;
        if (ObjectUtil.isNotNull(bankAccount)) {
            bankAccountDTO = DozerConvert.copyProperties(bankAccount, BankAccountDTO.class);
        }
        return bankAccountDTO;
    }

    @Override
    public int save(BankAccountDTO bankAccountDTO) {
        BankAccount bankResult = bankAccountMapper.selectByUserId(bankAccountDTO.getUserId());
        BankAccount bankAccount = new BankAccount();
        BeanUtils.copyProperties(bankAccountDTO, bankAccount);
        if(bankResult == null){
            bankAccount.setId(uidGenerator.getUID());
            bankAccount.setCreateTime(DateUtils.nowTime());
            bankAccount.setUpdateTime(DateUtils.nowTime());
            bankAccount.setIsVerify(new Byte("0"));
           return bankAccountMapper.insert(bankAccount);
        }else{
            bankAccount.setUpdateTime(DateUtils.nowTime());
           return bankAccountMapper.update(bankAccount);
        }
    }


}
